package com.common.web.dataset.context;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.common.base.persistence.Page;
import com.common.web.dataset.description.DataSetDescription;
import com.common.web.dataset.vo.ColsVO;
import com.common.web.dataset.vo.RowsVO;

public final class DataSetUtil {

	private static final DataSetContext context = new DataSetContext();

	private DataSetUtil(){
		throw new AssertionError();
	}
	/**
	 * 将List集合对象转为Page相关的Map对象
	 * @param page
	 * @param clazz
	 * @return
	 * @throws ClassNotFoundException
	 */
	public static <T> Page<T> listToPage(Page<T> page,Class<?> clazz) throws ClassNotFoundException {
		return listToPage(page, clazz, "");
	}
	/**
	 * 将List集合对象转为Page相关的Map对象
	 * @param page
	 * @param clazz
	 * @param name
	 * @return
	 * @throws ClassNotFoundException
	 */
	public static <T> Page<T> listToPage(Page<T> page,Class<?> clazz,String name) throws ClassNotFoundException {
		DataSetDescription description = context.getDescription(clazz);
		List<ColsVO> cols = description.toDataSetColList(name);

		List<RowsVO> rows = context.toDataSetRowList(page,description,cols,clazz);

		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("cols", cols);
		jsonMap.put("rows", rows);
		page.setList(null);
		page.setDataset(jsonMap);
		return page;
	}
}
